﻿/**********************************************
* 作    者：YuBing
* 创建日期：2009年02月20日
* 描    述：Logic层通用类
* 修改日期：2009年07月06日
***********************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using log4net;

namespace Meqs.Logic
{
    /// <summary>
    /// Logic层通用类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="TId"></typeparam>
    public abstract class CommonLogic<T, TId> : BaseSqlMapDao
    {
        private readonly ILog _log = LogManager.GetLogger("DatabaseOp");

        /// <summary>
        /// 保存对象
        /// </summary>
        /// <param name="entity"></param>
        public virtual TId CreateObject(T entity)
        {
            try
            {
                string statementName = typeof(T).Name + ".Insert";
                return (TId)ExecuteInsert(statementName, entity);
            }
            catch (Exception ex)
            {
                _log.Error("保存对象出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 自定义保存
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="obj"></param>
        public virtual object InsertCustomer(string statementName, object obj)
        {
            try
            {
                return ExecuteInsert(statementName, obj);
            }
            catch (Exception ex)
            {
                _log.Error("自定义保存对象出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="entity"></param>
        public virtual int UpdateObject(T entity)
        {
            try
            {
                string statementName = typeof(T).Name + ".Update";
                return ExecuteUpdate(statementName, entity);
            }
            catch (Exception ex)
            {
                _log.Error("更新对象出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 自定义更新
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="obj"></param>
        public virtual int UpdateCustomer(string statementName, object obj)
        {
            try
            {
                return ExecuteUpdate(statementName, obj);
            }
            catch (Exception ex)
            {
                _log.Error("自定义更新对象出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        public virtual int DeleteObject(TId entityId)
        {
            try
            {
                string statementName = typeof(T).Name + ".Delete";
                return ExecuteDelete(statementName, entityId);
            }
            catch (Exception ex)
            {
                _log.Error("删除对象出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 自定义删除
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="obj"></param>
        public virtual int DeleteCustomer(string statementName, object obj)
        {
            try
            {
                return ExecuteDelete(statementName, obj);
            }
            catch (Exception ex)
            {
                _log.Error("自定义删除对象出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 删除全部对象
        /// </summary>
        /// <returns></returns>
        public virtual int DeleteAllObject()
        {
            try
            {
                string statementName = typeof(T).Name + ".Delete";
                return ExecuteDelete(statementName, null);
            }
            catch (Exception ex)
            {
                _log.Error("删除全部对象出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 查询单个对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public virtual T FindObjectById(TId entityId)
        {
            try
            {
                string statementName = typeof(T).Name + ".FindById";
                return ExecuteQueryForObject<T>(statementName, entityId);
            }
            catch (Exception ex)
            {
                _log.Error("查询单个对象出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 查询对象数量
        /// </summary>
        /// <returns></returns>
        public virtual int FindObjectByCount()
        {
            try
            {
                string statementName = typeof(T).Name + ".FindCount";
                return ExecuteQueryForObject<int>(statementName, null);
            }
            catch (Exception ex)
            {
                _log.Error("查询对象数量出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 查询对象数量
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public virtual int FindObjectByCount(Hashtable ht)
        {
            try
            {
                string statementName = typeof(T).Name + ".FindCount";
                return ExecuteQueryForObject<int>(statementName, ht);
            }
            catch (Exception ex)
            {
                _log.Error("查询对象数量出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        public virtual IList<T> FindObject(Hashtable ht)
        {
            try
            {
                string statementName = typeof(T).Name + ".Find";
                return ExecuteQueryForList<T>(statementName, ht);
            }
            catch (Exception ex)
            {
                _log.Error("查询列表出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 查询对象数量
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public virtual int FindCustomerByCount(string statementName, object obj)
        {
            try
            {
                return ExecuteQueryForObject<int>(statementName, obj);
            }
            catch (Exception ex)
            {
                _log.Error("查询对象数量出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        public virtual IList<T> FindAllObject()
        {
            try
            {
                string statementName = typeof(T).Name + ".Find";
                return ExecuteQueryForList<T>(statementName, null);
            }
            catch (Exception ex)
            {
                _log.Error("查询全部对象出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 自定义查询对象
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public virtual object FindCustomerByID(string statementName, object obj)
        {
            try
            {
                return ExecuteQueryForObject(statementName, obj);
            }
            catch (Exception ex)
            {
                _log.Error("自定义查询对象出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 自定义查询对象
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public virtual TCustomer FindCustomerByID<TCustomer>(string statementName, object obj)
        {
            try
            {
                return ExecuteQueryForObject<TCustomer>(statementName, obj);
            }
            catch (Exception ex)
            {
                _log.Error("自定义查询对象出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 自定义查询对象列表
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public virtual IList FindCustomer(string statementName, object obj)
        {
            try
            {
                return ExecuteQueryForList(statementName, obj);
            }
            catch (Exception ex)
            {
                _log.Error("自定义查询对象列表出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 自定义查询对象列表
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public virtual IList<TCustomer> FindCustomer<TCustomer>(string statementName, object obj)
        {
            try
            {
                return ExecuteQueryForList<TCustomer>(statementName, obj);
            }
            catch (Exception ex)
            {
                _log.Error("自定义查询对象列表出错：" + ex.Message, ex);
                throw;
            }
        }
    }
}
